***************************************************************
* Project: Defending the Watchdogs                            *
* Authors: Jonathan A. Solis  & Kelebogile Zvobgo             *
* Task: Monet Carlo Simulations (Appendix Section D, Table 5) *
* Date: 7/10/2022                                             *
* Software: Stata MP 17                                       *
***************************************************************

*-------------------------------------------------------------------------------
*NOTE: Please contact Jonathan (jsolis@aiddata.wm.edu) if you have any questions
*-------------------------------------------------------------------------------

*******************************************************************************************
*Instruction:
* -I would recommend to run entire script and then use the log file to see the results 
* -It takes me about an hour or so run the entire .do file
* -Each model output will have 59 Mean/SD pairs. First 11 are the model parameters in
*  the model. For example, posterior1 (poste~r1) is the coefficient/ standard error for 
*  Judicial Independence, etc.
*******************************************************************************************

log using mc_simulation06142022

*Set Working Directory
clear 
cd "[path]"

****************************************
****************************************

*************************
*-----------------------*
*MEDIA ATTACKS VARIABLES*
*-----------------------*
*************************

* -> I. CENSORSHIP

clear 
use DefendingWatchdogs_Dataset
set more off

*Natural log gdppc variables
gen gdppc_ln=ln(gdppc)

*Create Interactions*

*Judicial Independence (L&S)
gen lji_poly=lji*polyarchy

*IV variables (LJI): simple model
global xlist_simple_int       l.lji l.polyarchy l.lji_poly

*IV variables: controls 
global xlist_ctrls l.gmf_binary l.education l.KOFInGIdj l.gdppc_ln l.aid l.rents_wb l.conflict2 l.hrp_point

*set as panel
xtset ccode year

**************************************************************
********************MONTE CARLO SIMULATIONS*******************
**************************************************************

*Generate 1000 random draws for lji, polyarchy, and attacks variables*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

*----------------*
*Media Censorship*
*----------------*

*Set seed for reproducability
set seed 129727257

*Trd. Censorship-Elastic: Loop for 1000 draws
forvalues i = 1/1000 {
gen x`i'=rnormal(mdcensor,mdcensor_sd)
}

*****************************
**** Prep for the monte carlo
*****************************

      set more off
      set matsize 1000
	  
      *** Note that the matsize must be at least as large as the larger
      *** dimension in your posterior matrix, in this case 1000 rows.
      *** While this is possible in State SE and MP, Intercooled Stata
      *** puts an upper limit of 800 on matsize.  If you are using
      *** Stata IC, set matsize to n <= 800 and change the loop below to
      *** iterate from 1 to n, thus using only the first n draws
      *** of the UDS posterior sample.

	  
	  forvalues i=1/1000{
	  
	  display `i'
	 
        *** Fit the model, using the ith draw from the UDS posterior

*::::::: V-DEM: Attacks
*CENSORSHIP
quietly xtreg x`i' $xlist_simple_int $xlist_ctrls i.year, fe vce(robust)
			   
	  *** Extract the coefficients and variance-covariance matrix
        matrix b = e(b)
        matrix V = e(V)
        local blength = colsof(b)

        *** Preserve the dataset, take a single multivariate normal draw from the
        *** posterior distribution of the coefficients, and restore the dataset.
        *** We use the capture command to catch possible errors in drawnorm
        *** and drop these iterations gracefully.
        preserve
        capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
        if _rc == 0 {
          mkmat b1-b`blength', matrix(bsample)
          matrix posterior = nullmat(posterior) \ bsample
        }
        else {
          display "Error drawing sample...iteration dropped"
        }
        restore
      
	* } 

}
*
svmat posterior

*** Calculate means and standard deviations
tabstat posterior*, stat(mean sd)

*** Reset matrix size 
matrix drop _all
drop posterior*
 

* -> II. Harassment

clear 
use DefendingWatchdogs_Dataset
set more off

*Natural log gdppc variables
gen gdppc_ln=ln(gdppc)

*Create Interactions*

*Judicial Independence (L&S)
gen lji_poly=lji*polyarchy

*IV variables (LJI): simple model
global xlist_simple_int       l.lji l.polyarchy l.lji_poly

*IV variables: controls 
global xlist_ctrls l.gmf_binary l.education l.KOFInGIdj l.gdppc_ln l.aid l.rents_wb l.conflict2 l.hrp_point

*set as panel
xtset ccode year

*----------------*
*Media Harassment*
*----------------*

*Set seed for reproducability
set seed 32359752

*Media Harassment-Elastic: Loop for 1000 draws
forvalues i = 1/1000 {
gen x`i'=rnormal(mdharass,mdharass_sd)
}

*****************************
**** Prep for the monte carlo
*****************************

      set more off
      set matsize 1000
	  
      *** Note that the matsize must be at least as large as the larger
      *** dimension in your posterior matrix, in this case 1000 rows.
      *** While this is possible in State SE and MP, Intercooled Stata
      *** puts an upper limit of 800 on matsize.  If you are using
      *** Stata IC, set matsize to n <= 800 and change the loop below to
      *** iterate from 1 to n, thus using only the first n draws
      *** of the UDS posterior sample.

	  
	  forvalues i=1/1000{
	  
	  display `i'
	 
        *** Fit the model, using the ith draw from the UDS posterior

*::::::: V-DEM: Attacks
*HARASSMENT
quietly xtreg x`i' $xlist_simple_int $xlist_ctrls i.year, fe vce(robust)
			   
	  *** Extract the coefficients and variance-covariance matrix
        matrix b = e(b)
        matrix V = e(V)
        local blength = colsof(b)

        *** Preserve the dataset, take a single multivariate normal draw from the
        *** posterior distribution of the coefficients, and restore the dataset.
        *** We use the capture command to catch possible errors in drawnorm
        *** and drop these iterations gracefully.
        preserve
        capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
        if _rc == 0 {
          mkmat b1-b`blength', matrix(bsample)
          matrix posterior = nullmat(posterior) \ bsample
        }
        else {
          display "Error drawing sample...iteration dropped"
        }
        restore
      
	* } 

}
*
svmat posterior

*** Calculate means and standard deviations
tabstat posterior*, stat(mean sd)

*** Reset matrix size 
matrix drop _all
drop posterior*

* -> III. INTERNET CENSORSHIP

clear 
use DefendingWatchdogs_Dataset
set more off

*Natural log gdppc variables
gen gdppc_ln=ln(gdppc)

*Create Interactions*

*Judicial Independence (L&S)
gen lji_poly=lji*polyarchy

*IV variables (LJI): simple model
global xlist_simple_int       l.lji l.polyarchy l.lji_poly

*IV variables: controls 
global xlist_ctrls l.gmf_binary l.education l.KOFInGIdj l.gdppc_ln l.aid l.rents_wb l.conflict2 l.hrp_point

*set as panel
xtset ccode year

**************************************************************
********************MONTE CARLO SIMULATIONS*******************
**************************************************************

*Generate 1000 random draws for lji, polyarchy, and attacks variables*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

*---------------*
*Internet Censor*
*---------------*

*Set seed for reproducability
set seed 26115781

*Internet Censorship-Elastic: Loop for 1000 draws
forvalues i = 1/1000 {
gen x`i'=rnormal(inetcen,inetcen_sd)
}

*****************************
**** Prep for the monte carlo
*****************************

      set more off
      set matsize 1000
	  
      *** Note that the matsize must be at least as large as the larger
      *** dimension in your posterior matrix, in this case 1000 rows.
      *** While this is possible in State SE and MP, Intercooled Stata
      *** puts an upper limit of 800 on matsize.  If you are using
      *** Stata IC, set matsize to n <= 800 and change the loop below to
      *** iterate from 1 to n, thus using only the first n draws
      *** of the UDS posterior sample.

	  
	  forvalues i=1/1000{
	  
	  display `i'
	 
        *** Fit the model, using the ith draw from the UDS posterior

*::::::: V-DEM: Attacks
*INTERNET CENSORSHIP
quietly xtreg x`i' $xlist_simple_int $xlist_ctrls i.year, fe vce(robust)
			   
	  *** Extract the coefficients and variance-covariance matrix
        matrix b = e(b)
        matrix V = e(V)
        local blength = colsof(b)

        *** Preserve the dataset, take a single multivariate normal draw from the
        *** posterior distribution of the coefficients, and restore the dataset.
        *** We use the capture command to catch possible errors in drawnorm
        *** and drop these iterations gracefully.
        preserve
        capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
        if _rc == 0 {
          mkmat b1-b`blength', matrix(bsample)
          matrix posterior = nullmat(posterior) \ bsample
        }
        else {
          display "Error drawing sample...iteration dropped"
        }
        restore
      
	* } 

}
*
svmat posterior

*** Calculate means and standard deviations
tabstat posterior*, stat(mean sd)

*** Reset matrix size 
matrix drop _all
drop posterior*

***********************
*---------------------*
*JUDICIAL INDEPENDENCE*
*---------------------*
***********************

clear 
use DefendingWatchdogs_Dataset
set more off

*Natural log gdppc variables
gen gdppc_ln=ln(gdppc)

*Create Interactions*

*Judicial Independence (L&S)
gen lji_poly=lji*polyarchy
*gen lji_poly1=lji1*polyarchy1

*IV variables (LJI): simple model
global xlist_simple_int l.lji l.polyarchy l.lji_poly

*Create lags for MC Simulations
sort ccode year 
by ccode: gen lji1 = lji[_n-1]
by ccode: gen lji_sd1 = lji_sd[_n-1]
by ccode: gen polyarchy1 = polyarchy[_n-1]
by ccode: gen polyarchy_sd1 = polyarchy_sd[_n-1]

*IV variables: controls 
global xlist_ctrls l.gmf_binary l.education l.KOFInGIdj l.gdppc_ln l.aid l.rents_wb l.conflict2 l.hrp_point

*set as panel
xtset ccode year


**************************************************************
********************MONTE CARLO SIMULATIONS*******************
**************************************************************

*Generate 1000 random draws for lji, polyarchy, and attacks variables*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

*---------*
*Jud Indep*
*---------*

*Set seed for reproducability
set seed 73963141

*LJI-Elastic: Loop for 1000 draws
forvalues i = 1/1000 {
gen x`i'=rnormal(lji1,lji_sd1)
}

* -> IV. JUDICIAL INDEPENDENCE
*     a. Media Censorship

*****************************
**** Prep for the monte carlo
*****************************

      set more off
      set matsize 1000
	  
      *** Note that the matsize must be at least as large as the larger
      *** dimension in your posterior matrix, in this case 1000 rows.
      *** While this is possible in State SE and MP, Intercooled Stata
      *** puts an upper limit of 800 on matsize.  If you are using
      *** Stata IC, set matsize to n <= 800 and change the loop below to
      *** iterate from 1 to n, thus using only the first n draws
      *** of the UDS posterior sample.

	  
	  forvalues i=1/1000{
	  
	  display `i'
	 
        *** Fit the model, using the ith draw from the UDS posterior

*::::::: LINZER & STATON
*JUDICIAL INDEPENDENCE - MEDIA CENSROSHIP

quietly xtreg mdcensor c.x`i'##c.polyarchy1 $xlist_ctrls i.year, fe vce(robust)
			   
	  *** Extract the coefficients and variance-covariance matrix
        matrix b = e(b)
        matrix V = e(V)
        local blength = colsof(b)

        *** Preserve the dataset, take a single multivariate normal draw from the
        *** posterior distribution of the coefficients, and restore the dataset.
        *** We use the capture command to catch possible errors in drawnorm
        *** and drop these iterations gracefully.
        preserve
        capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
        if _rc == 0 {
          mkmat b1-b`blength', matrix(bsample)
          matrix posterior = nullmat(posterior) \ bsample
        }
        else {
          display "Error drawing sample...iteration dropped"
        }
        restore
      
	* } 

}
*
svmat posterior

*** Calculate means and standard deviations
tabstat posterior*, stat(mean sd)

*** Reset matrix size 
matrix drop _all
drop posterior*

* -> IV. JUDICIAL INDEPENDENCE
*     b. Media Harassment

*****************************
**** Prep for the monte carlo
*****************************

      set more off
      set matsize 1000
	  
      *** Note that the matsize must be at least as large as the larger
      *** dimension in your posterior matrix, in this case 1000 rows.
      *** While this is possible in State SE and MP, Intercooled Stata
      *** puts an upper limit of 800 on matsize.  If you are using
      *** Stata IC, set matsize to n <= 800 and change the loop below to
      *** iterate from 1 to n, thus using only the first n draws
      *** of the UDS posterior sample.

	  
	  forvalues i=1/1000{
	  
	  display `i'
	 
        *** Fit the model, using the ith draw from the UDS posterior

*::::::: LINZER & STATON
*JUDICIAL INDEPENDENCE - MEDIA HARASSMENT

quietly xtreg mdharass c.x`i'##c.polyarchy1 $xlist_ctrls i.year, fe vce(robust)
			   
	  *** Extract the coefficients and variance-covariance matrix
        matrix b = e(b)
        matrix V = e(V)
        local blength = colsof(b)

        *** Preserve the dataset, take a single multivariate normal draw from the
        *** posterior distribution of the coefficients, and restore the dataset.
        *** We use the capture command to catch possible errors in drawnorm
        *** and drop these iterations gracefully.
        preserve
        capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
        if _rc == 0 {
          mkmat b1-b`blength', matrix(bsample)
          matrix posterior = nullmat(posterior) \ bsample
        }
        else {
          display "Error drawing sample...iteration dropped"
        }
        restore
      
	* } 

}
*
svmat posterior

*** Calculate means and standard deviations
tabstat posterior*, stat(mean sd)

*** Reset matrix size 
matrix drop _all
drop posterior*

* -> IV. JUDICIAL INDEPENDENCE
*     c. Internet Censorship

*****************************
**** Prep for the monte carlo
*****************************

      set more off
      set matsize 1000
	  
      *** Note that the matsize must be at least as large as the larger
      *** dimension in your posterior matrix, in this case 1000 rows.
      *** While this is possible in State SE and MP, Intercooled Stata
      *** puts an upper limit of 800 on matsize.  If you are using
      *** Stata IC, set matsize to n <= 800 and change the loop below to
      *** iterate from 1 to n, thus using only the first n draws
      *** of the UDS posterior sample.

	  
	  forvalues i=1/1000{
	  
	  display `i'
	 
        *** Fit the model, using the ith draw from the UDS posterior

*::::::: LINZER & STATON
*JUDICIAL INDEPENDENCE - INTERNET CENSROSHIP

quietly xtreg inetcen c.x`i'##c.polyarchy1 $xlist_ctrls i.year, fe vce(robust)
			   
	  *** Extract the coefficients and variance-covariance matrix
        matrix b = e(b)
        matrix V = e(V)
        local blength = colsof(b)

        *** Preserve the dataset, take a single multivariate normal draw from the
        *** posterior distribution of the coefficients, and restore the dataset.
        *** We use the capture command to catch possible errors in drawnorm
        *** and drop these iterations gracefully.
        preserve
        capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
        if _rc == 0 {
          mkmat b1-b`blength', matrix(bsample)
          matrix posterior = nullmat(posterior) \ bsample
        }
        else {
          display "Error drawing sample...iteration dropped"
        }
        restore
      
	* } 

}
*
svmat posterior

*** Calculate means and standard deviations
tabstat posterior*, stat(mean sd)

*** Reset matrix size 
matrix drop _all
drop posterior*

***********
*---------*
*POLYARCHY*
*---------*
***********

clear 
use DefendingWatchdogs_Dataset
set more off

*Natural log gdppc variables
gen gdppc_ln=ln(gdppc)

*Create Interactions*

*Judicial Independence (L&S)
gen lji_poly=lji*polyarchy
*gen lji_poly1=lji1*polyarchy1

*IV variables (LJI): simple model
global xlist_simple_int l.lji l.polyarchy l.lji_poly

*Create lags for MC Simulations
sort ccode year 
by ccode: gen lji1 = lji[_n-1]
by ccode: gen lji_sd1 = lji_sd[_n-1]
by ccode: gen polyarchy1 = polyarchy[_n-1]
by ccode: gen polyarchy_sd1 = polyarchy_sd[_n-1]

*IV variables: controls 
global xlist_ctrls l.gmf_binary l.education l.KOFInGIdj l.gdppc_ln l.aid l.rents_wb l.conflict2 l.hrp_point

*set as panel
xtset ccode year


**************************************************************
********************MONTE CARLO SIMULATIONS*******************
**************************************************************

*Generate 1000 random draws for lji, polyarchy, and attacks variables*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

*---------*
*Polyarchy*
*---------*

*Set seed for reproducability
set seed 888918322

*Polyarchy-Elastic: Loop for 1000 draws
forvalues i = 1/1000 {
gen x`i'=rnormal(polyarchy1,polyarchy_sd1)
}

* -> IV. POLYARCHY
*     a. Media Censorship

*****************************
**** Prep for the monte carlo
*****************************

      set more off
      set matsize 1000
	  
      *** Note that the matsize must be at least as large as the larger
      *** dimension in your posterior matrix, in this case 1000 rows.
      *** While this is possible in State SE and MP, Intercooled Stata
      *** puts an upper limit of 800 on matsize.  If you are using
      *** Stata IC, set matsize to n <= 800 and change the loop below to
      *** iterate from 1 to n, thus using only the first n draws
      *** of the UDS posterior sample.

	  
	  forvalues i=1/1000{
	  
	  display `i'
	 
        *** Fit the model, using the ith draw from the UDS posterior

*::::::: VDEM
*POLYARCHY - MEDIA CENSROSHIP

quietly xtreg mdcensor c.lji1##c.x`i' $xlist_ctrls i.year, fe vce(robust)
			   
	  *** Extract the coefficients and variance-covariance matrix
        matrix b = e(b)
        matrix V = e(V)
        local blength = colsof(b)

        *** Preserve the dataset, take a single multivariate normal draw from the
        *** posterior distribution of the coefficients, and restore the dataset.
        *** We use the capture command to catch possible errors in drawnorm
        *** and drop these iterations gracefully.
        preserve
        capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
        if _rc == 0 {
          mkmat b1-b`blength', matrix(bsample)
          matrix posterior = nullmat(posterior) \ bsample
        }
        else {
          display "Error drawing sample...iteration dropped"
        }
        restore
      
	* } 

}
*
svmat posterior

*** Calculate means and standard deviations
tabstat posterior*, stat(mean sd)

*** Reset matrix size 
matrix drop _all
drop posterior*

* -> IV. POLYARCHY
*     b. Media Harassment

*****************************
**** Prep for the monte carlo
*****************************

      set more off
      set matsize 1000
	  
      *** Note that the matsize must be at least as large as the larger
      *** dimension in your posterior matrix, in this case 1000 rows.
      *** While this is possible in State SE and MP, Intercooled Stata
      *** puts an upper limit of 800 on matsize.  If you are using
      *** Stata IC, set matsize to n <= 800 and change the loop below to
      *** iterate from 1 to n, thus using only the first n draws
      *** of the UDS posterior sample.

	  
	  forvalues i=1/1000{
	  
	  display `i'
	 
        *** Fit the model, using the ith draw from the UDS posterior

*::::::: VDEM
*POLYARCHY - MEDIA HARASSMENT

quietly xtreg mdharass c.lji1##c.x`i' $xlist_ctrls i.year, fe vce(robust)
			   
	  *** Extract the coefficients and variance-covariance matrix
        matrix b = e(b)
        matrix V = e(V)
        local blength = colsof(b)

        *** Preserve the dataset, take a single multivariate normal draw from the
        *** posterior distribution of the coefficients, and restore the dataset.
        *** We use the capture command to catch possible errors in drawnorm
        *** and drop these iterations gracefully.
        preserve
        capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
        if _rc == 0 {
          mkmat b1-b`blength', matrix(bsample)
          matrix posterior = nullmat(posterior) \ bsample
        }
        else {
          display "Error drawing sample...iteration dropped"
        }
        restore
      
	* } 

}
*
svmat posterior

*** Calculate means and standard deviations
tabstat posterior*, stat(mean sd)

*** Reset matrix size 
matrix drop _all
drop posterior*

* -> IV. POLYARCHY
*     c. Internet Censorship

*****************************
**** Prep for the monte carlo
*****************************

      set more off
      set matsize 1000
	  
      *** Note that the matsize must be at least as large as the larger
      *** dimension in your posterior matrix, in this case 1000 rows.
      *** While this is possible in State SE and MP, Intercooled Stata
      *** puts an upper limit of 800 on matsize.  If you are using
      *** Stata IC, set matsize to n <= 800 and change the loop below to
      *** iterate from 1 to n, thus using only the first n draws
      *** of the UDS posterior sample.

	  
	  forvalues i=1/1000{
	  
	  display `i'
	 
        *** Fit the model, using the ith draw from the UDS posterior

*::::::: LINZER & STATON
*JUDICIAL INDEPENDENCE - INTERNET CENSROSHIP

quietly xtreg inetcen c.lji1##c.x`i' $xlist_ctrls i.year, fe vce(robust)
			   
	  *** Extract the coefficients and variance-covariance matrix
        matrix b = e(b)
        matrix V = e(V)
        local blength = colsof(b)

        *** Preserve the dataset, take a single multivariate normal draw from the
        *** posterior distribution of the coefficients, and restore the dataset.
        *** We use the capture command to catch possible errors in drawnorm
        *** and drop these iterations gracefully.
        preserve
        capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
        if _rc == 0 {
          mkmat b1-b`blength', matrix(bsample)
          matrix posterior = nullmat(posterior) \ bsample
        }
        else {
          display "Error drawing sample...iteration dropped"
        }
        restore
      
	* } 

}
*
svmat posterior

*** Calculate means and standard deviations
tabstat posterior*, stat(mean sd)

*** Reset matrix size 
matrix drop _all
drop posterior*

log close
translate mc_simulation06142022.smcl mc_simulation06142022.log 
